import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue()
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      // 使用空 stub 替换 xmlbuilder2 以解决浏览器兼容性问题
      'xmlbuilder2': fileURLToPath(new URL('./src/utils/empty-stub.ts', import.meta.url))
    },
    dedupe: ['gl-matrix']
  },
  optimizeDeps: {
    include: ['js-3d-model']
  },
  build: {
    rollupOptions: {
      output: {
        manualChunks: undefined
      }
    },
    commonjsOptions: {
      transformMixedEsModules: true
    }
  },
  server: {
    host: '0.0.0.0',
    port: 5175,
    cors: true,
    headers: {
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin'
    },
    proxy: {
      '/api/orthanc': {
        target: 'http://106.14.195.137/orthanc',
        ws: true,
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api\/orthanc/, '')
      }
    }
  },
  define: {
    global: 'globalThis',
    'process.env': {}
  },
  worker: {
    format: 'es'
  }
})
